//
//  ListSortController.m
//  MClient
//
//  Created by  zhouyi on 11-3-20.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "ListSortController.h"
#import "MCUIToolkit.h"

#define ALPHA_ARRAY [NSArray arrayWithObjects: @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil]

@implementation  ListSortController
@synthesize labelArray;

//每一行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	int row = [indexPath row];
	
	NSMutableArray *cellInfos = [MCUIToolkit getAListInfo:listLayout
												 listData:self.listData
												 rowIndex:row
												cellWidth:cellWidth];
	[aListInfo setObject:cellInfos forKey:[NSNumber numberWithInt:row]];
	if ([cellInfos count] <= 0) {
		return 0.0;
	}
	cellHeight = [[[cellInfos objectAtIndex:([cellInfos count] - 1)] objectForKey:@"cellHeight"] floatValue];
	if (cellHeight < imageHeight) {
		cellHeight = imageHeight + 12.0;
		return cellHeight;
	}
	if (cellHeight == 0.0) {
		return cellHeight;
	}
	if (cellHeight < 28.0) {
		return 52.0;
	}
	return cellHeight + 12.0;
}

//初始化TableVie根据参数和数据绘制每一行以及填充数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	int row = [indexPath row];
	UITableViewCell *cell = [MCUIToolkit getAListCell:[aListInfo objectForKey:[NSNumber numberWithInt:row]]
										  imagesCache:imagesCache
											 rowIndex:row
										isMultiSelect:NO
											cellWidth:cellWidth
										   cellHeight:cellHeight
										   imageWidth:imageWidth
										  imageHeight:imageHeight];
	NSMutableDictionary *record = [self.listData objectAtIndex:indexPath.row];
	NSMutableArray *dataButtons = [record objectForKey:@"btn"];
	NSMutableArray *cfgButtons =  [listLayout objectForKey:@"dbtn"];
	NSMutableArray *descButtons = [MCUIToolkit getButtons:dataButtons cfgButtons:cfgButtons];
	cell.accessoryType = UITableViewCellAccessoryNone;
	if (descButtons && [descButtons count] > 0) {
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
	} else {
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
	}	
	return cell;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{	/*
	NSMutableArray *sortArray = [[NSMutableArray alloc] init];
	NSMutableArray *listFields = [listLayout objectForKey:@"item"];
	for (int i = 0; i < [listData count]; i++) {
		NSMutableDictionary *record = [listData objectAtIndex:i];
		for (int j = 0; j < [listFields count]; j++) {
			NSMutableDictionary *appItem = [listFields objectAtIndex:j];
			if ([[appItem objectForKey:@"v"] boolValue]) {
				NSString *fieldValue = [MCUIToolkit getFieldTextValue:record appItem:appItem selectOption:nil];
				if ([fieldValue length] > 0) {
					bool flag = YES;
					for (NSString *sort in sortArray) {
						if ([sort isEqualToString: [fieldValue substringToIndex:1]]) {
							flag = NO;
						}
					}
					if (flag) {
						[sortArray addObject:[fieldValue substringToIndex:1]];
					}
					break;
				}
			}
		}
	}
	return sortArray;*/
	return ALPHA_ARRAY;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{
	NSInteger count = 0;
	for(NSString *character in labelArray) {
		if([[character substringToIndex:1] isEqualToString:title]) {
			if ([self.tableview numberOfRowsInSection:0] > count) {
				NSIndexPath *indexPath = [NSIndexPath indexPathForRow:count inSection:0];
				[self.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
			}
			return count;
		}
		count++;
	}
   	return -1;
}

NSInteger sortFunction(NSMutableDictionary *record1, NSMutableDictionary *record2, void *context) {
   	NSMutableArray *listFields = [(NSMutableDictionary *)context objectForKey:@"item"];
	NSString *fieldValue1 = @"";
	NSString *fieldValue2 = @"";
	for (int i = 0; i < [listFields count]; i++) {
		NSMutableDictionary *appItem = [listFields objectAtIndex:i];
		if ([[appItem objectForKey:@"v"] boolValue]) {
			fieldValue1 = [MCUIToolkit getFieldTextValue:record1 appItem:appItem selectOption:nil];
			fieldValue2 = [MCUIToolkit getFieldTextValue:record2 appItem:appItem selectOption:nil];
			break;
		}
	}
	return [[MCUIToolkit getChineseSpell:fieldValue1] compare:[MCUIToolkit getChineseSpell:fieldValue2]
													  options:NSCaseInsensitiveSearch | NSLiteralSearch];
}

- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	[super getDataFromServerSuccess:returnValue];
	
	[listData sortUsingFunction:sortFunction context:listLayout];
	
	labelArray = [[NSMutableArray alloc] init];
	NSMutableArray *listFields = [listLayout objectForKey:@"item"];
	for (int i = 0; i < [listData count]; i++) {
		NSMutableDictionary *record = [listData objectAtIndex:i];
		for (int j = 0; j < [listFields count]; j++) {
			NSMutableDictionary *appItem = [listFields objectAtIndex:j];
			if ([[appItem objectForKey:@"v"] boolValue]) {
				NSString *fieldValue = [MCUIToolkit getFieldTextValue:record appItem:appItem selectOption:nil];
				[labelArray addObject:fieldValue];
				break;
			}
		}
	}
	[self reloadData];
}

- (void) dealloc {
	[labelArray release];
	[super dealloc];
}
@end
